I have an array inside a foreach to generate data in json, but I should add a comma to validate the code. But I can not ... how can I do it?
$obj = array(
'name' => 'value',
'img' => 'value',
'url' => 'value',
);
echo json_encode($obj);
I have this code
{"name":"value","img":"value","url":"value"}
{"name":"value","img":"value","url":"value"}
{"name":"value","img":"value","url":"value"}
but I would like this code
[
{"name":"value","img":"value","url":"value"},
{"name":"value","img":"value","url":"value"},
{"name":"value","img":"value","url":"value"}
]
Don't echo the JSON in the loop. Put all the objects in another array, and convert that to JSON.
Start with an empty array:
$array = [];
In the loop push onto that array:
$array[] = array(
'name' => 'value',
'img' => 'value',
'url' => 'value',
);
After the loop is done, do:
echo json_encode($array);
Related
I have an array like the following:
array(
'session_id' => 'ea29e7ae5c976794896b4c256f455dd5',
'user_identifier' => "{'user_id':87,'username':'some username','email':'someuseremail.com','first_name':'Some','last_name':'User','company':'Company'}",
'request_uri' => '/'
);
And I would like to convert it to the following:
array(
'session_id' => 'ea29e7ae5c976794896b4c256f455dd5',
'user_id' => 87,
'username' => 'some username',
'email' => 'someuseremail.com',
'first_name' => 'Some',
'last_name' => 'User',
'company' => 'Company',
'request_uri' => '/'
);
Which means I am decoding the JSON at user_identifier key and I am making part of the initial array $original and then I am removing the user_identifier key.
So far this is what I have done:
foreach ($original as $key => $log) {
$original[$key] = (array) $log;
}
foreach ($original as $key => $log) {
foreach($log as $k => $v) {
if ($k === 'user_identifier') {
$original['decoded'] = (array) json_decode($v);
}
}
}
Which is giving me an array like this one:
array(
'session_id' => 'ea29e7ae5c976794896b4c256f455dd5',
'request_uri' => '/',
'user_identifier' => "{'user_id':87,'username':'some username','email':'someuseremail.com','first_name':'Some','last_name':'User','company':'Company'}",
'decoded' => array(
'user_id' => 87,
'username' => 'some username',
'email' => 'someuseremail.com',
'first_name' => 'Some',
'last_name' => 'User',
'company' => 'Company'
)
);
As you may notice this is not even the array I am looking for and I have already one foreach loop to convert the initial result to an array - it's coming as and stdClass object - and then a nested foreach loop for decode the JSON and try to make it part of the initial array.
In such case I will need to add another loop to linearize the array. My concern is this array is just an example but the one I need to convert is a big one.
Is there any better way to achieve this?
I am using PHP 5.3.3
I would do it like this:
Extract the JSON from the original into an array. (Be sure to set the second argument of json_decode so you end up with an array instead of an object.)
$identifier = json_decode($your_array['user_identifier'], true);
merge the extracted array with the original.
$your_array = array_merge($your_array, $identifier);
Unset the now-redundant JSON
unset($your_array['user_identifier']);
I have a problem where I am getting data from database and need to put that in an array. The array is associative and I am not sure about this practice so I thought I should ask the community. Is this the correct way of adding data to array? The array is for the radio buttons that will be provided to the helper class in prestashop. The array structure is important. This is the var_dump array structure which I have in $options_break2.
$options_value = array();
$options = array();
for($z=0; $z<sizeof($options_break2); $z++)
{
$options_value = array_push($options_value,
array(
"id" => $options_break2[$z],
"name" => $options_break2[$z],
"label" => $options_break2[$z],
)
);
}
$options = array_push($options, $options_value);
What I want is that the array should contain something like:
$example = array(
array(
'id_option' => 'some value',
'name' => 'some value',
),
array(
'id_option' => 'some value',
'name' => 'some value',
),
);
Actually you don't need to use array_push and array() if your PHP version is above 5.6, and you can improve your loop by using the foreach loop:
$options_value = [];
foreach ($options_break2 as $opt) {
$options_value[] = [
"id_option" => $opt, // some_value
"name" => $opt // some_value
];
}
$options = $options_value; // you don't really need this
I am getting this error "Trying to get property of non-object" for lines
'price' => $product->product_price,
'name' => $product->product_name
for doing in the same page
function remove($rowid) {
$this->cart->update(array(
'rowid' => $rowid,
'qty' => 0
));
}
i can solve this problem, by doing like, 'price' => $product['product_price'],Bus as my other page using 'price' => $product->product_pricethem as fine, so i dont want to convert it to array,
my question is how can i convert $this->cart->update(array( to an object so that, these lines
'price' => $product->product_price,
'name' => $product->product_name
works fine for object?
Thanks in advance.
$Arr = array(
'rowid' => $rowid,
'qty' => 0
);
$Obj = (object)$Arr;
This is conversion of Array to Object. Do you want it?
You assign the result to an array. Then you try to use the array as an object. Next, the returned result is an array as well which you try to use as a object.
The smarter ways you can convert array into object
From PHP manual:
<?php
$literalObjectDeclared = (object) array(
'foo' => (object) array(
'bar' => 'baz',
'pax' => 'vax'
),
'moo' => 'ui'
);
print $literalObjectDeclared->foo->bar; // outputs "baz"!
?>
little genius hack:
<?php
// assuming $var is a multidimensional array
$obj = json_decode (json_encode ($var), FALSE);
?>
So you can convert by casting array into object:
<?php
$array = array(
// ...
);
$object = (object) $array;
?>
if you want manual way you can do this:
<?php
$object = object;
foreach($arr as $key => $value)
{
$object->{$key} = $value;
}
?>
Define $product as a new standard class:
$product = new stdClass();
I want to create this JSON string with PHP:
[{name:'20140722.1304',data:[[0, 0.224],[0, 0.228]] }, {name:'20140729.1149',data:[[1, 0.224],[1,0.228]] }]
My current attempt:
$jsonArray = array(
'name' => '20140722.1304'
,'data' => array('0' => '0.024', '1'=> '0.028')
);
$jsonValue = json_encode($jsonArray);
echo $jsonValue;
But this code's output looks like:
{"name":"20140722.1304","data":["0.024","0.028"]}
Where did I went wrong? What do I have to change in my code to get to my expected output?
I finally got you to tell us what you want in the comments; please be up-front with this in the future. The expected output you gave differed from your actual output in so many ways that it was impossible to tell what you actually thought the problem was, but:
I want to get the output as {name:'20140722.1304',data:[[0, 0.224],[0, 0.228]]}
At this point, the only difference I can see is that your data is a nested array in your expected output, but not your actual output.
That has nothing to do with JSON. You're just not building your input array correctly.
Try json-encoding this:
$jsonArray = array(
'name' => '20140722.1304'
,'data' => array(array(0, 0.024), array(0, 0.028))
);
<?php
for($i=0;$i<2;$i++)
{
$jsonArray[] = array(
'name' => '20140722.1304'
,'data' => array('0' => '0.024', '1'=> '0.028')
);
}
//$jsonValue = json_encode($jsonArray);
$jsonValue = json_encode($jsonArray,true);
echo$jsonValue;
?>
try this
$jsonArray = array(
'name' => '20140722.1304'
,'data' => array('0' => '0.024', '1'=> '0.028')
);
$out = array_values($jsonArray);
echo json_encode($out);
output:
["20140722.1304",["0.024","0.028"]]
EDit:
$array = array(
2 => array("name" => '20140722.1304'),
4 => array("'data" => array('0' => '0.024', '1'=> '0.028')));
$out = array_values($array);
echo json_encode($out);
output:
[{"name":"20140722.1304"},{"'data":["0.024","0.028"]}]
Let's say I have an array formatted like so:
$data = array(
'variables' => array(
'823h9fhs9df38h4f8h' => array(
'name' => 'Foo',
'value' => 'green'
),
'sdfj93248fhfhf88rh' => array(
'name' => 'Bar',
'value' => 'red'
)
)
);
Say I wanted to access the name & values of each array in the variables array. Surely you can access it just looping over the main variables array and not looping over each individual item array? Something like so?
foreach ($data as $k => $v) {
$name = $data['variables'][0]['name'];
}
I'm sure I'm missing something simple...
You can do
foreach ($data['variables'] as $k => $v) {
$name = $v['name'];
}
You can also try this
create a new array containing just the names..
$new_arr = array_column($data['variables'],'name' );
echo $new_arr[0].'<br/>';
echo $new_arr[1].'<br/>';