How to manipulate this array? - php

I am a beginner in PHP. I am trying to make an operation in this array. I want to insert this array in my database like on to many in a table.But before the insertion i have to modify the array values.
this is my array.
$services=[0=>('id_e'=>91701,'id_s'=03),
1=>('id_e'=>'','id_s'=>01),
2=>('id_e'=>'','id_s'=>02)
];
It has to become like as follow.
$services=[0=>('id_e'=>91701,'id_s'=>03),
1=>('id_e'=>'91701','id_s'=>01),
2=>('id_e'=>'91701','id_s'=>02)
];
And then i want insert into the database. Any idea please?

Try this:
$id_e = null;
foreach ($services as &$row) {
if ($row['id_e']) $id_e = $row['id_e'];
else $row['id_e'] = $id_e;
}
unset($row);
demo

Related

How to put foreach in array

I have this
$homedata = $user->getbanks();
foreach ($homedata as $data)
echo json_encode(array("replies" => array(array("message" => $data->bankname))));
I have this put the result am getting is only one array from the list. I want to get all.
If I understand your requirement correctly, you need the banknames in an array replies within a key message. For that the below code will do the job. But you can optimize the query for better performance.
$homedata = $user->getbanks();
$banks['replies'] = [];
foreach ($homedata as $data) {
$banks['replies'][]['message'] = $data->bankname;
}
print_r(json_encode($banks)); exit;
Further, you were only getting the first content because you are overriding the previous content when you iterate using foreach.

php encode nested json in wrong format

I trying to test display a nested Json structure but the result is in wrong format.
php
while($row=$statement->fetch()){
$value=str_replace('"','',$row['item_option_value']);
$option[$row['item_option_name']]=explode(',',$value);
$all[$row['oid']]=$option;
}
echo json_encode($all);
mysql databas structure
Here is the result when i run the script.
I want the json structure to be the right side of the screenshot. Anyone knows what's wrong?
You would need to empty the $option arrays then like this:
$option = []; //or $option = array(); in PHP < 5.4
This is needed in order not to keep storing the data from the previous iteration.
So:
while($row=$statement->fetch()){
$value=str_replace('"','',$row['item_option_value']);
$option = [];
$option[$row['item_option_name']]=explode(',',$value);
$all[$row['oid']]=$option;
}
echo json_encode($all);
The problem is because of this line here,
$option[$row['item_option_name']]=explode(',',$value);
In each iteration of while() loop, you're appending the previously calculated $options array to $all. Instead, create an temporary array to hold intermediate result and append that to $all in each iteration, like this:
while($row=$statement->fetch()){
$opArray = array();
$value=str_replace('"','',$row['item_option_value']);
$opArray[$row['item_option_name']]=explode(',',$value);
$all[$row['oid']]=$opArray;
}
echo json_encode($all);

Merge multiple arrays by index

Im some array that's being returned by some query.. and the result is something like this:
array(array('balance_1'=> '-5', 'balance_2'=>'-21'), array('balance_1'=> '-21', 'balance_2'=>'21'), array('balance_1'=> '-50', 'balance_2'=>'40'))
i want to transform this into an array that looks something like this:
array(array(-5,11,-50), array(-21, 21, 40));
basicly i want to join all balance_1, all balance_2, all balance_3 into separated arrays.
any ideas? thanks
You'll just loop over the list, then collect the values. It's most simple if you reuse the existing keys to group:
foreach ($list as $row) {
foreach ($row as $key=>$value) {
$out[$key][] = $value;
}
}
This way you'll get an $out array, with [balance_1] or [balance_2] holding the value lists.
Loop though the array and use "array_key_exists" if the key exists add to the array, if it doesn't build a new array with your index.
For more can be found here:
http://www.php.net/manual/en/function.array-key-exists.php

Using foreach to fetch $_POST elements

Im trying to fetch my form date in one for loop and then put the data into the database. My problem is that i have'nt worked that much with PHP myself and i cant figgure out how to put the data into the database.
Here is the code
<?php
function get_post_information() {
$inf[] = array();
foreach($_POST as $field => $value) {
$inf[$field] = $value;
}
return $inf;
}
I execute the code like this:
get_post_information();
But then what? How do i get this into my database?
Hope anyone can help :)
If you want to store the complete array in one single database column, you should use the serialize() function to make a string from your array. There also is an unserialize() function to convert it to an array again.

Returning an array of objects in a php function

I have multiple rows getting returned from a database query.
I am able to get just a row at a time, but I want to put the rows in an array of objects like this:
$trailheads[] = new StdClass;
Loop
{
$trailheads[] = $trailhead; // Put each object into the array of objects
}
But when I try to loop through each array, I am having trouble extracting the values of each row. What is the right way to loop through the returned object and get the values?
The code that does not work is pretty basic:
$returned_obj->o->trailhead_name
But I am actually hoping be able to loop through each array element.
Maybe try casting to array in your for loop, this is untested and may not work as is but should get you on the right track:
foreach ($trailheads as (array) $key){
// $key is now an array, recast to obj if you want
$objkey = (object) $key;
}
i'm assuming your using mysql since you did not say...
you can use thsi function mysql_fetch_object()
<?php
$trailheads = array()
$result = mysql_query("select * from mytable");
while ($row = mysql_fetch_object($result, 'trailhead')) {
$trailheads[] = $row;
}

Categories