how to i get to the arrays content if it doesnt have a key like this
$products[0]
this will get me partially there but how to i get past []
( [0] => Array
( [] => Array (
[0] => Array ( [product_name] => stuff i need to get to )
That is very strange. You could try
$products[0][''][0]['product_name']
Use var_dump or var_export to print your array and you will see its an empty string.
print_r will give you output like that for empty strings.
$products[0][""][0]["product_name"]
Related
How to extract only value from the below array
Array ( [COUNT(department_name)] => 3 )
this is a response from a db query the actual value came like this
Array ( [0] => Array ( [COUNT(department_name)] => 3 ) )
if we select the 0th index $department[0] it is giving this
Array ( [COUNT(department_name)] => 3 )
but now i need only the value from this i.e 3 how can i extract the value from this.
and i tried like this $department[0]['department_name'] and $department[0]['COUNT(department_name)'] but no result.
You can use this simply and it's working -
$department = var_export($department,true);
print_R($department[0]['COUNT(department_name)'])
var_export helps you get the structured information of a variable.
For more see this -
var_export
NOTE - I will suggest you to to get a more readable key from database.
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;
I'm getting a response from a service and print_r() shows an array but it seems to be different from usual arrays.
If I print array_values it's are empty.
PHP:
print_r($token);
//Result: Array ( [{"access_token":"123","token_type":"bearer"}] => )
print_r(array_values($token));
//Result: Array ( [0] => )
Why are the access_token and token_type values not listed in array_values?
The answer is not JSON, It's not because you have a JSON type array. It is because there is NO value in your array.
//Result: Array ( [{"access_token":"123","token_type":"bearer"}] => )
That array has one index with no value, hence array_values shows nothing. You have created that array incorrectlly :)
I can reproduce your outputs with this:
$token = array('{"access_token":"123","token_type":"bearer"}' => '');
That is because you have an array with the key
'{"access_token":"123","token_type":"bearer"}'
but no value.
To access the JSON string in the array key, you could do this:
$keys = array_keys($token);
print_r($keys[0]);
To access the JSON object, you can further do
print_r(json_decode($keys[0]));
Output:
(
[access_token] => 123
[token_type] => bearer
)
Demo: Fiddle
When a print_r() an array $stats, I get the following:
Array ( [0] => Array ( [like] => 71 [dislike] => 372 [total] => 443 [like_s] => 78 [dislike_s] => 291 [total_s] => 369 [final] => 11 ))
I want to get the [dislike_s] value and put it into a variable.
I have attempted this:
$statss = $stats['dislike_s'];
But it did not work. I have also tried $statss = $stats['dislike_s'][0]; without result.
What am I doing wrong?
You are missing a level of your array
Array ( [0] => Array ( [like] => 71
//^ this level
Also you are using the wrong variable, as you said your array is stored in $stats so
$total_revision = $stats[0]['dislike_s'];
$stats is a two dimensional Array, i.e. it is an array of arrays. You can see this from the output of print_r. You have something that looks like `Array ( [0]=>Array(...)). Therefore, when accessing an element of the inside array, you can think of it like this:
$inner_array=$stats[0];
$total_revisions=$inner_array['dislike_s'];
Php gives you a shorthand for combining these steps(pretty much all languages do) and it looks like $total_revisions=$stats[0]['dislike_s'] but intuitively it's the same thing. You're saying "in the array $stats[0] give back the value of array element 'dislike_s'"
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.