How to print arrays inside array by Laravel blade? - php

My function returns an array to a variable in laravel blade page like this :
Array
(
[id] => 1
[name] => Enterpise
[children] => Array
(
[0] => Array
(
[id] => 2
[name] => name1
[children] => Array
(
[0] => Array
(
[id] => 5
[name] => name2
[children] =>
)
[1] => Array
(
[id] => 6
[name] => name3
[children] => Array
(
[0] => Array
(
[id] => 11
[name] => name4
[children] =>
)
.....
Now I want to print these values inside my page by using something like foreach. Consider that their children's numbers are not specified, it is impossible to do that inside of my function. It is important to me to be done in blade. How can I do that?

In your blade files you can use any php functions like print_r().
Example:
{{ print_r($array) }}

As this is very old issue but I found json directive very useful.
<pre>
<code>
#json($array);
</code>
</pre>

You can find the way for your problem over here:
https://laracasts.com/discuss/channels/general-discussion/passing-arrays-to-blade-and-iterating
Or you can use:
{{ echo json_encode($yourarray) }}
but this will print your array as json encoded string.

You can use PHP directive
#php
echo "<pre>";
print_r($array);
echo "</pre>";
#endphp

I always use this clean and simple solution that outputs your array in a nicely formatted way, using JSON:
<pre><code>{{ json_encode($array, JSON_PRETTY_PRINT) }}</code></pre>
You can drop the JSON_PRETTY_PRINT if you want it displayed on a single line:
<pre><code>{{ json_encode($array) }}</code></pre>

the best output and less code has print_r() :
<pre>
{{print_r($array)}}
</pre>

Related

Output the imtId value

Please tell me how to output the imtId value, the array is not complete, I will not output further, but the meaning should be clear.Thank you in advance
Array
(
[id] => mavrin-wildberries-1635334576193516728
[jsonrpc] => 2.0
[result] => stdClass Object
(
[cards] => Array
(
[0] => stdClass Object
(
[id] => d3c33a3f-f5b3-5647-8e7a-ad50d27d4417
[imtId] => 30306963
[userId] => 0
[supplierId] => e9b901b9-b663-5648-97b8-6313d0e245ba
[imtSupplierId] => 0
I tried:
echo ['result']['cards'][0]['nmId']
A few things:
You need to echo an actual variable, not just a series of indexes.
The item inside "result" is an object, not an array
So is the item within the "0" index.
There is no such index as "nmld" - you said you wanted "imtId" instead, so I don't know why you didn't use that?
Therefore, if this data is contained in a variable called $arr then something like
echo $arr["result"]->cards[0]->imtId;

PHP: Removing string count of entire array from serialized array

I have a JSON array that I'm saving in a PHP variable. I'm then serializing that array using serialize($variable) and saving it to the dB with the built in Wordpress function update_post_meta().
The problem I'm having is that the entire serialized array is wrapped with a string count. Like so, currently being saved as:
s:332:"a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}";
I need to save it without the string count for the entire array. Desired output:
a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}};
Any help with this is, as always, greatly appreciated.
It seems that your array is serialized twice and that's what gives you the addendum ... I have taken your serialize data and un serialized it twice and it came back as you wanted :
<?php
$ser = 's:332:"a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}";';
$arr = unserialize($ser);
echo '<pre>';
print_r($arr); /* Print after one unserialize */
echo '<pre>';
print_r(unserialize($arr)); /* Print with unserialize to the once unserialized*/
Will return:
a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}
Array
(
[0] => Array
(
[type] => weeks
[cost] => 3
[modifier] =>
[base_cost] => 2
[base_modifier] =>
[from] => 1
[to] => 4
)
[1] => Array
(
[type] => persons
[cost] => 6
[modifier] =>
[base_cost] => 5
[base_modifier] =>
[from] => 1
[to] => 2
)
)
As you can see only after two unserialize it returns back to an array... so just do one and you have what you need.

Can you retrieve nested array by name in php

I have an array like this:
(
[data] => Array
(
[account_id] => 1
[description] => my asset
[value] => Estimate
[value_amount] => 85000
[type] => Vehicle
[owner] => Array
(
[app_id] => 123
[percent] => 100
)
)
)
Clearly I can loop through the array and pull out the nested owner array that way, but is there something similar to array_column that will get the entire owner nested array without having to loop ?
Use the indexes, no function necessary.
$owner = $array['data']['owner']
or..
$percent = $array['data']['owner']['percent']
In php it's call associative array which mean index will not numeric it can be anythink.
So you can retrieve data like
<?php echo $array['data']['owner']['app_id']; ?>

PHP: How to extract a property from this array

I have an array of values returned from Facebook - let's call it $array.
If I do print_r($array) - it looks like this:
Array
(
[code] => 200
[headers] => Array
(
[0] => Array
(
[name] => Some value
[value] => *
)
[1] => Array
(
[name] => Some value
[value] => Some value
)
[2] => Array
(
[name] => Some value
[value] => Some value
)
)
[body] => {"about":"Some more values.","can_post":true}
)
I need to extract the body part from this array.
I cannot refer to it by it's position, I'm looking for something like $array->body and receive the {....} string.
$array->body would work if the variable $array was an object
For arrays, just use:
$body = $array['body'];
(see: http://be2.php.net/manual/en/language.types.array.php)
If you want to access to your array via -> just do 1 more step:
$array = (object) $array;
And now, you can access to your body via:
$array->body;
Else without this step there is just one way:
$array['body'];
If you are more interested about converting arrays into objects, you can visit this question: How to convert an array to object in PHP?
Access array elements by using their name.
$array['body'];

How can I turn an array to csv file in php

One more beginner question:
how can I turn an array (actually it's just a text) like that:
Array
(
[0] => Array
(
[0] => nsor#cg.ukrtel.net
[1] => p2007#rambler.ru
[2] => pan20072009#yandex.ru
[3] => gf#ukr.net
[4] => tmkp#ma.odessa.ua
[5] => export#soh.by
[6] => advert#soh.by
)
)
into a .csv file by means of PHP? This file should be without things like this "[0] =>", only addresses.
Assuming your array is in a variable called $arr, you could use
echo implode("\r\n",$arr);
And you might want to prefix it with
header('Content-type: text-csv; charset="UTF-8"');
or similar.
Look at the first example at http://php.net/manual/en/function.fputcsv.php
Use your array instead of the array in the example.

Categories