2

Well I have something like

$Arr1 = array("a1" => array("a1b", "a1b"),
              "a2" => array("a2b", "a2b"),
              "a3",
              "a4",
              "a5" => array("a5b", "a5b")
);

meaning that "a3" and "a4" are keys without values.

I need to go through a foreach loop to get $key => $value pairs.

Should be something with checking of isset($value) but it doesn't work.

UPDATE: Question is closed. Thanks to all. As it was written key without value is not a key, but value with a default integer key. So if anyone wants to use the structure above make this

 foreach ($Arr1 as $key => $value) { 
      if (is_int($key)) { 
           $key = $value; 
           $value = null; 
      } 
      //use $key and $value
 }
giacoder
  • 980
  • 8
  • 21
  • 1
    issset won't work - it's isset – treyBake Jan 15 '19 at 11:12
  • 4
    another note: a3 is not a key, but a value as there is no label with it – treyBake Jan 15 '19 at 11:13
  • 2
    What is your expected output? – Web Artisan Jan 15 '19 at 11:16
  • @Quasimodo's clone if you don't know an answer or don't understand, don't write anything. Your "WTF" is not needed here at all. Find another social media for that. By the way, Maxim Fedorov already gave a correct answer that works if you are interested. – giacoder Jan 15 '19 at 12:04
  • If you feel insulted, I apologize. I'm not a native speaker and for now I understood that as common speak like "Oh sh..", "Oh damn" since it is spoken/written almost everywhere in any context, not personally addressed. My US friend does so not only once a day. :) – Pinke Helga Jan 15 '19 at 12:20

3 Answers3

3

Each element of an array has a key. "a3" and "a4" aren't keys, they are elements which have numeric keys. You make sure it if you make var_dump of this array

array (size=5)
  'a1' => 
    array (size=2)
      0 => string 'a1b' (length=3)
      1 => string 'a1b' (length=3)
  'a2' => 
    array (size=2)
      0 => string 'a2b' (length=3)
      1 => string 'a2b' (length=3)
  0 => string 'a3' (length=2)
  1 => string 'a4' (length=2)
  'a5' => 
    array (size=2)
      0 => string 'a5b' (length=3)
      1 => string 'a5b' (length=3)

You can get elements with numeric keys with array_filter function and checking of key type (for example with help is_int function)

$arr = array(
    "a1" => array("a1b", "a1b"),
    "a2" => array("a2b", "a2b"),
    "a3",
    "a4",
    "a5" => array("a5b", "a5b")
);

$newArr = array_filter($arr, function($key) {
    return is_int($key);
}, ARRAY_FILTER_USE_KEY);

or foreach statement:

$arr = array(
    "a1" => array("a1b", "a1b"),
    "a2" => array("a2b", "a2b"),
    "a3",
    "a4",
    "a5" => array("a5b", "a5b")
);

$newArr = [];
foreach ($arr as $key => $value) {
  if (is_int($key)) {
      $newArr[] = $value;
  }
}
Maksym Fedorov
  • 6,383
  • 2
  • 11
  • 31
  • Thanks a lot. I came to the same thing foreach ($Arr1 as $key => $value) { if (is_numeric($key)) { $key = $value; $value = null; } //using $key } – giacoder Jan 15 '19 at 11:58
2

Your array looks strange since you mixup keys and values like "a#". It is equivalent to:

$Arr1 = array
(
  "a1" => array("a1b", "a1b"),
  "a2" => array("a2b", "a2b"),
  0    => "a3",
  1    => "a4",
  "a5" => array("a5b", "a5b")
);

I guess you meant this one instead:

$Arr1 = array
(
  "a1" => array("a1b", "a1b"),
  "a2" => array("a2b", "a2b"),
  "a3" => null,
  "a4" => null,
  "a5" => array("a5b", "a5b")
);

Then you can access it with foreach:

foreach ($Arr1 as $key => $value)
  echo "$key => " .
       ( is_array($value)
           ? implode(', ', $value)
           : $value
       ),
       '<br>', PHP_EOL
  ;

Or if you do not want keys with empty values:

foreach ($Arr1 as $key => $value)
  if(is_array($value))
    echo "$key => ", implode(', ', $value), '<br>', PHP_EOL;

or just

$filered = array_filter( $Arr1, function($v) { return is_array($v); } );

foreach ( $filered as $key => $sub_array)
  echo $key, ' => ', implode(', ', $sub_array), '<br>', PHP_EOL;
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
0

Why dont you set the values of "a3" and "a4" to empty array ("=> array()")? Then you can loop through and can access the values with the "ax" keys. In a loop you process the data, except the array is empty.

Jnt0r
  • 174
  • 3
  • 16