I'm having a problem exploding an array
Here's my code
$arr1 = array();
$i=1;
foreach ($out1 as $value2){
$arr1[][$i]= array_merge((array)$value2,(array)$detail);
$i++;
}
and this is the output
Array(
[0] => Array(
[id] => 1234
[name] => Rick Roll
[dept] => IT)
[1] => Array(
[id] => 1234
[name] => Dave Roll
[dept] => IT)
)
but when i try to explode the array it gives me error message
Warning: explode() expects parameter 2 to be string, array given
here's the code
$data = explode(","$array)
$q = "INSERT INTO ".TBL_ARE_ENTRY." VALUES(null,'$id[1]','$name[2]','$dept[3]')";
You do not need to use explode. i think this is what you want:
$q = "INSERT INTO ".TBL_ARE_ENTRY."
VALUES(null,''$array[0][0]','$array[0][1]',''$array[0][2]')";
here is my code
array 1:
Array
(
[0] => Array
(
[id] => 42166
[Company_Website] => http://www.amphenol-highspeed.com/
[company_name] => Amphenol High Speed Interconnect
[city_name] => New York
[country_name] => USA
[comp_img] =>
)
)
array 2:
Array
(
[0] => Array
(
[Product_Name] => CX to CX,Amphenol High Speed Active,Serial Attached SCSI
[company_id] => 42166
)
)
php code:
$total = count($result);
$i=0;
foreach ($result as $key=>$value) {
$i++;
$company_id= implode(",",(array)$value['id']);
if ($i != $total)
echo',';
}
code to fetch array 2:
foreach ($res as $key1=>$value1) {
echo $total;
$event[$value['company_name']] = $value1['Product_Name'];
if($value1['company_id']==$company_id )
{
echo " match";
//$key[['company_name']]= $value1['Product_Name'];
}
else
{
echo "not matched";
}
}
what i need create a new index if company_id is match with id of another array.
that is product_name.
if product name is there just create index otherwise show null.
i want show in key=> value .
output should be like:
Array
(
[0] => Array
(
[id] => 42166
[Company_Website] => http://www.amphenol-highspeed.com/
[company_name] => Amphenol High Speed Interconnect
[city_name] => New York
[country_name] => USA
[comp_img] =>
[Product_Name] => CX to CX,Amphenol High Speed Active,Serial Attached SCSI
)
)
Your all problems with keys in arrays will disappear when you will start using company ids as a keys.
To reindex you arrays, you can use:
$array1 = array_combine(array_column($array1, 'id'), $array1);
$array2 = array_combine(array_column($array2, 'company_id'), $array2);
In the output you will get:
array 1:
Array
(
[42166] => Array
(
[id] => 42166
...
)
)
And array 2 will looks similiar - id as a key.
So accessing to the elements using ids as a keys is a piece of cake right now.
I need some help. I have a variable containing this string;
[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]
I need to retrieve the id and value for each pair and make it any array in PHP.
You can use json_decode and pass the second param as true so it returns an array like this
$json = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$decoded = json_decode($json,true);
print_r($decoded);
Working Example
Output would be
Array
(
[0] => Array
(
[id] => 17
[value] => 123456789
)
[1] => Array
(
[id] => 18
[value] => 2012-06-13
)
[2] => Array
(
[id] => 19
[value] => Kampala
)
[3] => Array
(
[id] => 20
[value] => 1
)
.......
)
Which you can loop through using foreach like.
foreach($decoded as $de){
// access id with $de['id']
// access value with $de['value']
}
You have got an json string. You can convert it to an array by using function json_decode
Check this code .
$str = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"}, {"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$array = json_decode($str);
foreach($array as $temp){
echo "ID : ".$temp->id."\t Value: ".$temp->value;
echo "<br />";
}
I have a result array that comes back from CodeIgniter looking like this:
Array
(
[ILS] => ils
[0] => Array
(
[laser_id] => 1
[sort_order] => 1
[laser_name] => ILS12.75
[laser] => ils1275
[image] => ils1275
[link] => ils1275
)
[1] => Array
( .. )
etc.
}
I used array_merge to get the ILS item into the array, but I need it to get it into the array[0]. I also need to get a similar item into array[1] and so on in the result array. How do I do this? Here is how I got the ILS item in:
function get_laser_configs()
{
$this->db->order_by('sort_order');
$query = $this->db->get('all_lasers')->result_array();
$ils = array('ILS' => 'ils');
return array_merge($ils, $query);
}
No need for array_merge, simply do:
$query[0]['ILS'] = 'ils';
If you want to do it for each element in the array:
foreach ($query as $key => $q) {
$query[$key]['ILS'] = 'ils';
}
I'm grabbing a some XML data and parsing it with PHP.
Most of the results come in multidimensional array but occasionally I'll get a result in a single array and it breaks my script.
I'm trying to format the single result to match the format of the results in the multidiminsonal array but I'm not having any luck.
Here is what I got:
Array
(
[name] => Steve Jobs
[id] => 3
)
Array
(
[0] => Array
(
[name] => Steve Jobs
[id] => 6
)
[1] => Array
(
[name] => Bill Gates
[id] => 8
)
)
I'm trying to format the single result to match the multidimensional format then flatten...
Array
(
[0] => Array
(
[name] => Steve Jobs
[id] => 3
)
[1] => Array
(
[name] => Steve Jobs
[id] => 6
)
[2] => Array
(
[name] => Bill Gates
[id] => 8
)
)
I've tried this:
$array_check = #array_keys($result[0]['name']);
if ($array_check[0] == "0") {
$result;
} elseif ($array_check[0] == "name") {
$ReWrite = array ([0] =>
array (['name']=>
array ($result[0]['name'])
));
$result = $ReWrite;
}
I thought that would do it but it's off...
Try this:
$array_check = #array_keys($result[0]['name']);
if (!isset($array_check[0])) {
$result[] = $array_check;
} else {
$result = $array_check;
}
var_dump($result);
If your first array was assigned to the variable $singleArray and your target results stored in $results, try this array_push($results, $singleArray);
Use this together with reset() it returns the first element of an array:
if(!is_array(reset($result))){
array_push($results, $result);
}
This will test if the array does not contain an array as an element, if it doesn't push the whole array to an aggregate array.
Edit: Try this loop:
for($i = 0; $i <= count($multi); $i++){
$arr = $multi[$i];
if(!is_array($arr)){
$multi[$i] = array($arr);
}
} var_dump($multi);