0

I struggle to access specific item within my PHP array, which is created like this array('expected' => array('form', 'title'))

.Array
(
    [expected] => Array
        (
            [0] => form
            [1] => title
        )

)

I would like to access the title of the first array (expected) as well as the value of the element within this array (form and title) I tried methods such as array_values()or key but I never get the right results.

EDIT Thanks to Aamir, this problem is solved. In fact, it was because I pass the array as a parameter into a method and I set it to null by default. Yes I know, dumb stuff.

2
  • 3
    what should be the output you want? Commented Feb 26, 2016 at 10:57
  • I just want to access the element I cited. Fro instance, print "expected", "form" and "title". Commented Feb 26, 2016 at 10:58

5 Answers 5

2

The question is vague enough to be answered by: Use a RecursiveTreeIterator

<?php
$x = array(
    'level1' => array(
        'item1.1',
        'level2'=>array(
            'item2.1',
            'item2.2',
            'level3'=>array(
                'item3.1'
            )
        ),
        'item1.2'
    )
);

$it = new RecursiveTreeIterator( new RecursiveArrayIterator($x), RecursiveIteratorIterator::SELF_FIRST );
foreach($it as $line) {
    echo $line, PHP_EOL;
}

prints

\-Array
  |-item1.1
  |-Array
  | |-item2.1
  | |-item2.2
  | \-Array
  |   \-item3.1
  \-item1.2

You might want to refine your question....

Sign up to request clarification or add additional context in comments.

Comments

1
foreach($array as $key => $value){
 echo $key; //expected
 echo $value[0]; //form
 echo $value[1]; //title

 //OR if you have more values then 
foreach ($value as $key1 => value1){
  echo $value1; //form in 1st iteration and title in 2nd iteration
 }}

Comments

0

Try this :

   $array = array('expected' => array
        (
            0 => 'form',
        1 => 'title',
        )

);
$expected= $array['expected'];
$form = $expected[0];
$title = $expected[1];

Comments

0

try this:

<?php
$array = array('expected' => array('form', 'title'));
function  testFunc($array)
{

  foreach ($array as $key=>$value) {

      if(is_string($key))
      {
          echo $key."<br>";
      }

       if(is_string($value))
      {
          echo $value."<br>";
      }
    if(is_array($value))
    {
        testFunc($value);
    }

}  
}
testFunc($array);
?>

output:

expected
form
title

Comments

0

Use below code:-

$my_array = Array
(
    'expected' => Array
        (
            '0' => 'form',
            '1' => 'title'
        )

);

echo $form =  $my_array[key($my_array)][0];  // print form 
echo $title = $my_array[key($my_array)][1];  //print title 

Hope it will help you :)

1 Comment

Thanks. Unfortunately, I do not know what the key of the Array (here "expected") will be, therefore I cannot use it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.