How to access an un-keyed element of an array? - php

I have an array that comes from a CMS, which means I can't change how it comes to me. The array is named $master_menu; this is the print_r:
Array
(
[A] => Array
(
[ ] => Appetizer
[PROD] => Array
(
[AC] => Order Anchovies
[AL] => Side Alfredo Sauce
[AO] => Add On
)
)
)
I have a variable called $class that contains 'A'. I know I can get at the entire A sub-array like this:
$master_menu[$class]
and I could get at the PROD sub-array like this:
$master_menu[$class]['PROD']
But how can I just get the value in the sub-array without a key (value is Appetizer in this sample)? I've tried $master_menu[$class][0], but obviously that doesn't work because there isn't a sub-array with a zero index.

The empty index is a space $master_menu["A"][" "]. Try using var_dump instead of print_r, it has more details.

There's no such thing as an element without a key. Maybe the key is " " ? I think that'd be consistent with your print_r output.

It looks like print_r gave you a space as an index. Try:
$master_menu[$class][" "]

You can use array_values and parse that out.
Edit: It looks like you might be able to access the empty key ' '... perhaps $master_menu[$class][' '].. just a thought as I'm not sure the exact output.

Related

get value within object in an array

I have an array, containing an object. I need the value of a property of the first object but somehow I get an empty value.
My array $params (from print_r) looks like this:
Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...
Cut off here, there are two more objects in this array.
Now if I do: echo $params[0]->name I get an empty result.
Also tried print_r($params[0], true);, empty result.
Also tried, empty result:
$status = $params[0];
echo $status->name;
What am I doing wrong here?
Thanks in advance
Well, as you said your array looks like this :
Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...
So there is no $param[0], you should do $param['newOrderStatus'] and then get what you want : $param['newOrderStatus']->name
You need to access object as following
$params['newOrderStatus'];
In above object you will have all child objects so you can access them by following
$params['newOrderStatus']->name;
$params['newOrderStatus']->template;
Your array $params has a key called newOrderStatus which has the object as a value you are looking for.
Looking at your example, there is value for index 0.
To get the value of the name property, you could use:
$params['newOrderStatus']->name
You can type cast it to an array like this:
$array = (array) $yourObject;

using array_keys and getting unexpected result

I have an array that is assigned to $elements. When I use array_keys to get the keys, I get what you would expect.
print_r(array_keys($elements));
Results in:
Array
(
[0] => anchor-namecontentblock_areaBlock0contentblock11_1
[1] => anchor-namecontentblock_areaBlock0contentblock22_1
[2] => anchor-namecontentblock_areaBlock0contentblock33_1
...
But when I try to use array_keys with a search value, I get an empty array.
print_r(array_keys($elements, "anchor-namecontentblock_areaBlock0contentblock11_1"));
Should the result not be:
Array
(
[0] => 0
)
Am I missing something?
You're doing the wrong array_keys search. Your anchor-name... values are KEYS in the original array, not VALUES. As such, your array_keys search argument is useless - it'll be searching the values of the original array, e.g.
$foo = array(
'anchor-namecontentblock_areaBlock0contentblock11_1' => 'somevalue'
etc..
searched by array_keys---------^^^^^^^^^^
You'd need do something more like:
$results = array_search('anchor-name...', array_keys($elements)));
^^^^^^^^^^^^^
instead.
Specifying a search parameter to array_keys allows you to retrieve the key(s) corresponding to one or more values in your array. You're passing it one of your array keys, so the function is returning no results.

Bring multi-dimensional array down (unnest) one level

Althoug this works well enough, I am curious if anyone knows of a prettier way of doing this as this situation seems to come up quite often.
<?php
//Initialy, data is nested up in $some_array[0] ...
$some_array = array(array('somevar' => "someValue", "someOtherVar" => "someOtherValue"));
print_r($some_array);
Array ( [0] => Array ( [somevar] => someValue [someOtherVar] => someOtherValue ) )
// Could the following line be achieved a more elegant fashion?
$some_array = $some_array[0];
print_r($some_array);
// Prints the intended result:
Array ( [somevar] => someValue [someOtherVar] => someOtherValue )
Does anyone know of a way to achieve this with a native function or in a more elegant fashion?
Thanks!
The native function you're looking for is called reset (Demo):
$some_array = reset($some_array);
For explicit clarification: current is not necessary.
You could use current (explained here), it basically points to the first element in the array and returns it.
To be absolutely sure you get the first element, you should reset your array, like so:
reset($arr)
$firstElement = current($arr)

Store an array to an array key

I'm trying to store an array ($temp) into the $data array, where key is prices.
$data['prices'] = $temp;
However, PHP converts the array into string instead and is throwing me and error.
Notice: Array to string conversion
Is $data = array('prices' => $temp); the only solution?
edit:
I found my mistake. Yes, $data was used previously as a string that's why PHP is converting the input into string.
Problem 2, doing a print_r on $data['prices'] = $xml->result->Prices->Price, shows only 1 set of array. But I am able to retrive 2 sets of result by doing a foreach loop on $data['prices']. Why is that so?
Content of $temp http://pastebin.com/ZrmnKUPB
Let me be more clear..
The full xml object I'm trying to extract information from: http://pastebin.com/AuMJiyrw
I'm only interested in the price array (Price_strCode and Price_strDescription) and store them in $data['prices']. End result something like this:
Array(
[0] => (
[Price_strCode] => 0001
[Price_strDescription] => Gold
)
[1] => (
[Price_strCode] => 0002
[Price_strDescription] => Silver
)
)
Unless you are doing some other array to string conversion elsewhere, the array is actually being stored as another array.
$data['prices'] will be an array, which can be accessed as $data['prices']['key'].
This is not possible, I have been doing this always and it works fine. You must be doing something else somewhere which is causing your array to convert to strong. share your code here

PHP built in array function

Lets say I end up with an array like such:
Array ( [0] => Array ( [0] => user
[1] => pass
)
)
maybe as a result of passing an array through a function and using func_get_args()
In this case, I would want to get rid of the initial array, so I just end up with:
Array ( [0] => user
[1] => pass
)
I know I could make a function to accomplish this, and push each element into a new array, however, is there some built in functionality with PHP that can pull this off?
$new_array = $old_array[0];
...
array_pop() will pop the last (first, if only 1 is present) element.
Just take the value of the first element of the "outer" array.
I would recommend array_shift as it removes and returns the first element off of the beginning of the array.

Categories