Here I want to create an array for query string like given below:
item[]['item_id']=I00001&item[]['rate']=10.52&item[]['qty']=2&
item[]['item_id']=I52124&item[]['rate']=15.00&item[]['qty']=1&
item[]['item_id']=I62124&item[]['rate']=8.20&item[]['qty']=5
I want to generate it dynamically.
for($i = 0 ; $i< count($allitems) ;$i++){
$itemid = explode('~',$allitems[$i]);
$arrdet[]=["'item_id'"=>$itemid[0],"'rate'"=>$itemid[1],"'qty'" =>$itemid[2]];
$item['item'] = array_merge($arrdet);
//$item['item'][]=["'item_id'"=>$itemid[0],"'rate'"=>$itemid[1],"'qty'" =>$itemid[2]];
}
echo http_build_query($item);
but my result for this
item[0]['item_id']=I00001&item[0]['rate']=10.52&item[0]['qty']=2&
item[1]['item_id']=I52124&item[1]['rate']=15.00&item[1]['qty']=1&
item[2]['item_id']=I62124&item[2]['rate']=8.20&item[2]['qty']=5
How it Possible?
Thanks in advance
I did so much workarounds. But, it should actually works.
$countAllitems = count($allitems);
$arr = array();
$items = array();
$query = array();
for($i = 0 ; $i< $countAllItems; $i++){
$itemid = explode('~',$allitems[$i]);
$arr['item_id'] = $itemid[0];
$arr['rate'] = $itemid[1];
$arr['qty'] = $itemid[2];
//Assign the array to another array with key 'item[]'
$items['item[]'] = $arr;
//Build the array to http query and assign to another array
$query[] = http_build_query($items);
}
//Implode the stored http queries
echo implode('&', $query);
The array is zero-indexed internally.
$array = [1, 2, 3];
is internally
$array = [
0 => 1,
1 => 2,
2 => 3
]
The http_build_query will always output keys of the array, even if you do not specify them explicitly.
Related
I have a code like this:
Lets assume that this arrays has this values:
$arr1 = array();
$arr2 = array();
$result = array();
$arr1[] = array( 'grade' => [1,2,3,4] );
$arr2[] = array( 'grade' => [1,2,3,4] );
foreach($arr1 as $a1){
$set1 = $a1['grade'];
foreach($arr2 as $a2){
$set2 = $a2['grade'];
}
$result[] = array('show_result' => $set1+$set2);
}
foreach{$result as $res){
echo $res['show_result'];
}
The output of the array $res['show_result'] must be:
2, 4, 6, 8
But I get the wrong addition of this arrays. Help will be much appreciated.
As Joni said, your first error is on line 3: ' should be ;
Then, you're not filling arrays like you wanted : array( 'grade' => 1,2,3,4 ); creates an array with first key is 'grade' with value '1', then second key is '0' with value '2' etc...
Your last foreach loop has a syntax error similar to your first error.
See a working correction here
$arr1 = array();
$arr2 = array();
$result = array();
array_push($arr1, 1, 2, 3, 4); //fill array with 4 values (integers)
array_push($arr2, 1, 2, 3, 4); //fill array with 4 values (integers)
//so $arr1 & $arr2 are now a 4 elements arrays
$length = count($arr1); //size of array, here 4
for ($i = 0; $i < $length; $i++) { //loop over arrays
array_push($result, ($arr1[$i] + $arr2[$i])); //fill the results array with sum of the values from the same position
}
var_dump($result);
You have quite a few syntax errors in your code.
Although this solution works, the idea behind using the same counter, $i, to extract a value from both arrays is brittle. For example, you'll get an Undefined offset if the first array has 5 grades instead of 4. If you take a step back and explain your problem in the larger context, perhaps we can provide a better solution. I get the sneaking suspicion you're asking an XY Problem.
http://sandbox.onlinephpfunctions.com/code/bb4f492c183fcde1cf4edd50de7ceebf19fe343a
<?php
$gradeList1 = ['grade' => [1,2,3,4]];
$gradeList2 = ['grade' => [1,2,3,4]];
$result = [];
for ($i = 0; $i < count($gradeList1['grade']); $i++) {
$first = $gradeList1['grade'][$i];
$second = $gradeList2['grade'][$i];
$result['show_result'][] = (int)$first + (int)$second;
}
var_dump($result);
I have a php string formed by images and corresponding prices like OK Like
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
I know that if I do:
$myArray = explode(',', $myString);
print_r($myArray);
I will get :
Array
(
[0] => ddb94-b_mgr3043.jpg
[1] => 3800
[2] => 83acc-b_mgr3059.jpg
[3] => 4100
)
But How could I split the string so I can have an associative array of the form?
Array
(
"ddb94-b_mgr3043.jpg" => "3800"
"83acc-b_mgr3059.jpg" => "4100"
)
Easier way to do like below:-
<?php
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
$chunks = array_chunk(explode(',', $myString), 2); //chunk array into 2-2 combination
$final_array = array();
foreach($chunks as $chunk){ //iterate over array
$final_array[trim($chunk[0])] = trim($chunk[1]);//make key value pair
}
print_r($final_array); //print final array
Output:-https://eval.in/859757
Here is another approach to achieve this,
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100,test.jpg,12321";
$arr = explode(",",$myString);
$temp = [];
array_walk($arr, function($item,$i) use (&$temp,$arr){
if($i % 2 != 0) // checking for odd values
$temp[$arr[$i-1]] = $item; // key will be even values
});
print_r($temp);
array_walk - Apply a user supplied function to every member of an array
Here is your working demo.
Try this Code... If you will receive all the key and value is equal it will work...
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
$myArray = explode(',', $myString);
$how_many = count($myArray)/2;
for($i = 0; $i <= $how_many; $i = $i + 2){
$key = $myArray[$i];
$value = $myArray[$i+1];
// store it here
$arra[$key] = $value;
}
print_r($arra);
I´m trying to insert arrays into other array, the problem is that when I call the array_push() method it overwrites the last one element of my array, then I just get an array with data of one array (the last one):
$users_data = [];
$resultSize = count($result);
$data = $result;
for ($i = 0; $i < $resultSize; $i++) {
$person = [
'nombre' => $result[$i]['nombre'],
'apellido' => $result[$i]['apellido'],
];
array_push($users_data, $person);
// $users_data = $person; I also have tried with this method.
};
I just receive one object with this:
Object {nombre: jane, apellido: doe}
What is going wrong?
It shud be like this,
$person['nombre'][$i] = $result[$i]['nombre'];
$person['apellido'][$i] = $result[$i]['apellido'];
^ you have missed this index.
Then no need of array_push(). you can directly assign persons to user_data
Or like this :
for ($i = 0; $i < $resultSize; $i++) {
$users_data['nombre'][] = $result[$i]['nombre'];
$users_data['apellido'][] = $result[$i]['apellido'];
};
I have a query in codeigniter like this
$query_tutors = $this->db->get_where("tutor_info", array('tutor_id' => $tutor_id));
I have also other array elements that I want to pass in the query which depends on some conditions.
So how do I push other multidimensional array elements to the existing array so I can pass the variable as a whole in the query?
array_push is not working in this case.
$array = array();
$array = array("tutor_id" => $tutor_id);
$array = array("online" => $online); // want to merge this to the 1st array.
$query_tutors = $this->db->get_where("tutor_info", $array);
First you're doing it wrong.
$array = array();
$array = array("tutor_id" => $tutor_id);
You're recreating the array again, which will delete it from the memory. Either you have to use
$array['tutor_id'] = $tutor_id;
$array["online"] = $online;
or
$array = array('tutor_id' => $tutor_id, 'online' => $online);
or if you want to merge two arrays
$array = array_merge(array('tutor_id' => $tutor_id), array('tutor_id' => $tutor_id));
Your initial code
$array = [];
$array = ["tutor_id" => $tutor_id];
Now, if you want to add conditional merge, simply follow,
if($condition)
{
$array = array_merge($array, ["online" => $online]);
}
If $condition == true You final array will be,
$array = ['tutor_id' => $tutor_id, 'online' => $online];
You are almost there, just need to read a little bit more about associative arrays.
Solution:
$array = array();
$array["tutor_id"] = $tutor_id;
$array["online"] = $online;
$query_tutors = $this->db->get_where("tutor_info", $array);
This way your $array will have all indexes which you want.
You can do something like this:
$array = array();
if (!empty($tutor_id))
{
$array["tutor_id"] = $tutor_id;
}
if (!empty($online))
{
$array["online"] = $online;
}
$query_tutors = $this->db->get_where("tutor_info", $array);
I hope my question adequately describes what I'm after...
Here is the situation. I have the following arrays with values.
categories['t-shirts'] = 10
categories['shorts'] = 11
...
clothing[0] = 't-shirts'
clothing[1] = 'shorts'
...
I want to replace the values in the clothing array (t-shirts, shorts) with the number that matches it from the categories array.
Cheers
foreach($clothing as $key => $val){
if(isset($categories[$val])){
$clothing[$key] = $categories[$val];
}
}
Codepad Example
You can use simple php
categories[clothing[0]] = "some value"
From your question, it looks like
$newArray=array_keys($originalArray);
should do the trick.
$count = count($clothing);
for($i=0; $i<$count; $i++)
$clothing[$i] = (array_key_exists($clothing[$i], $categories))
? $categories[$clothing[$i]] : 0;
for setting the $clothings without any count to 0
$categories = array();
$categories['t-shirts'] = 10;
$categories['shorts'] = 11;
$clothing = array();
$clothing[0] = 't-shirts';
$clothing[1] = 'shorts';
array_walk($clothing,
function(&$value) use($categories) {
if (isset($categories[$value]))
$value = $categories[$value];
}
);
var_dump($clothing);