1

I have an Array as shown bellow

 Array
    (
        [ifour consultancy 123] => Array
            (
                [Company] => ifour consultancy 123
                [Physical] => B-515, Gopal Palace, Near shiromani complex,
                [address] => test,
             )
    )

i am try to print it using

echo $array[0]['Company'];

and it give me Message:

Undefined offset: 0 instead of showing me ifour consultancy 123

4
  • your index is not "0" but "ifour consultancy 123". use a foreach loop instead. Commented Apr 29, 2016 at 12:15
  • is there any way so i can get it using $array[0]['Company']; and $array["ifour consultancy 123"]['Company']; ? Commented Apr 29, 2016 at 12:17
  • @Jkumar Try using array_values. See my answer. Commented Apr 29, 2016 at 12:20
  • 1
    @PraveenKumar yes it is working MonkeyZeus beat you by few seconds. Commented Apr 29, 2016 at 12:24

2 Answers 2

3

You can use the array_values() function to achieve your goal:

// Extract the values of the array and re-use as indexed array
$array = array_values($array);
echo $array[0]['Company'];

// If you want to keep your associative array as well then do this
$array = array_merge($array, array_values($array));
echo $array[0]['Company'];
// OR
echo $array['ifour consultancy 123']['Company'];
Sign up to request clarification or add additional context in comments.

Comments

1

For these kind of walkthroughs, you need to use foreach:

foreach ($array as $value) {
  print_r($value);
}

Or if you want to get 0 or number based indices, you need to use array_values():

$numbased = array_values($array);
$numbased[0]["Company"]; // ifour consultancy 123

Comments

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.