I declare the following array
$job_scope = array( "proposal_id",
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope",
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
I expect it to be created like
array([0] => 'proposal_id',
[1] => 'will_provide' => array([0] => "0",
[1] => "Supervision",
[2] => "Labor",
[3] => "Material",
[4] => "Equpment"),
[2] => 'general_scope',
[3] => 'per_bid' => array([0] => "Yes",
[1] => "No",
[2] => "Omit"),
[4] => 'job_type' => array([0] => "Painting",
[1] => "Sandblasting",
[2] => "Scappling")
But when I print the array it looks like
Array ( [0] => proposal_id [will_provide] => Array (
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment )
[1] => general_scope [per_bid] => Array (
[0] => Yes
[1] => No
[2] => Omit )
[job_type] => Array (
[0] => Painting
[1] => Sandblasting
[2] => Scappling )
I would like the array to be created in the same format as the second section of code.
All you need to do is assign an empty array to the proposal_id and general_scope. So the code will look like this
$job_scope = array( "proposal_id" => array(),
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope" => array(),
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
It will produce this array
Array (
[proposal_id] => Array ( )
[will_provide] => Array ( [0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
[general_scope] => Array ( )
[per_bid] => Array ( [0] => Yes
[1] => No
[2] => Omit
)
[job_type] => Array ( [0] => Painting
[1] => Sandblasting
[2] => Scappling
))
If you want to callback the value, (ex : call supervision value).
All you need to do is
print_r($job_scope['will_provide'][1])
and that will print the supervision value
use $new_job_scope = array_values($job_scope);
$job_scope = array(
"proposal_id",
"will_provide" => array(
"0",
"Supervision",
"Labor",
"Material",
"Equpment"
),
"general_scope",
"per_bid" => array(
"Yes",
"No",
"Omit"
),
"job_type" => array(
"Painting",
"Sandblasting",
"Scappling"
)
);
$new_job_scope = array_values($job_scope);
print_r($new_job_scope);
PhpFiddle
Create array first !!! Reassign at specified index with 2D array will be more clear to me
<?php
$arr = array('proposal_id','','general_scope','',''); //create array first
$arr[1] = array("will_provide" => array("0","Supervision","Labor","Material","Equpment"));
$arr[3] = array("per_bid" => array("Yes","No", "Omit"));
$arr[4] = array("job_type" => array("Painting","Sandblasting","Scappling"));
var_dump($arr);
?>
I think this process can serve you. I have just used a foreach loop to convert non-int key to int key:
$new_array = '';
foreach($job_scope as $k => $v){
if(is_int($k)){
$new_array[] = $v;
}else{
$new_array[] = [$k => $v];
}
}
print_r($new_array);
Output would be:
Array
(
[0] => proposal_id
[1] => Array
(
[will_provide] => Array
(
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
)
[2] => general_scope
[3] => Array
(
[per_bid] => Array
(
[0] => Yes
[1] => No
[2] => Omit
)
)
[4] => Array
(
[job_type] => Array
(
[0] => Painting
[1] => Sandblasting
[2] => Scappling
)
)
)
Related
I found lots of answers on stackoverflow and other sites relating to arrays but they don't seem to be working with my problem.
I have the following code:
Array (
"-10 to -5" => Array ( [0] => 44.78 [1] => 46.86 [2] => 20.64 )
"-20 to -15" => Array ( [0] => 8.01 [1] => 7.85 [2] => 21.08 )
"-5 to 5" => Array ( [0] => 3.5 [1] => 0.8 [2] => 0.12 )
"-15 to -10" => Array ( [0] => 43.25 [1] => 43.95 [2] => 56.02 )
"-25 to -20" => Array ( [0] => 0.45 [1] => 0.54 [2] => 2.15 )
);
How can I sort it by its keys so that it becomes something like:
Array (
"-25 to -20" => Array ( [0] => 0.45 [1] => 0.54 [2] => 2.15 )
"-20 to -15" => Array ( [0] => 8.01 [1] => 7.85 [2] => 21.08 )
"-15 to -10" => Array ( [0] => 43.25 [1] => 43.95 [2] => 56.02 )
"-10 to -5" => Array ( [0] => 44.78 [1] => 46.86 [2] => 20.64 )
"-5 to 5" => Array ( [0] => 3.5 [1] => 0.8 [2] => 0.12 )
);
Please suggest any solution. I tried alot and I still can't figure it out.
Thanks.
$data = [
'-10 to -5' => [ 44.78, 46.86, 20.64 ],
'-20 to -15' => [ 8.01, 7.85, 21.08 ],
'-5 to 5' => [ 3.5, 0.8, 0.12 ],
'-15 to -10' => [ 43.25, 43.95, 56.02 ],
'-25 to -20' => [ 0.45, 0.54, 2.15 ]
];
uksort($data, fn($key1, $key2) => strtok($key1, ' ') <=> strtok($key2, ' '));
print_r($data);
Try this:
ksort($array, SORT_NUMERIC);
I have two array. first one is multi dimensional array. second array contains key and value pair. now my goal is i want to check second index of every array from first array value to check that value in second array as key exist? if yes then that first array value need to replace with second array value.
$first_array = Array
(
[0] => Array
(
[0] => 2012/12
[1] =>
[2] => "SI"
[3] =>
[4] =>
[5] =>
)
[1] => Array
(
[0] => 2012/12
[1] =>
[2] => "MB"
[3] =>
[4] =>
[5] =>
)
)
$second_array = array(
["MB"] => "WE",
["SI"] => "SA",
["SO"] => "SA",
)
my output should look like this
$first_array = Array
(
[0] => Array
(
[0] => 2012/12
[1] =>
[2] => "SA"
[3] =>
[4] =>
[5] =>
)
[1] => Array
(
[0] => 2012/12
[1] =>
[2] => "WE"
[3] =>
[4] =>
[5] =>
)
)
Just go with simple foreach loop and if
Try this code snippet here
<?php
ini_set('display_errors', 1);
$first_array = Array
(
0 => Array
(
0 => "2012/12",
1 => "",
2 => "SI",
3 => "",
4 => "",
5 => "",
),
1 => Array
(
0 => "2012/12",
1 => "",
2 => "MB",
3 => "",
4 => "",
5 => "",
)
);
$second_array = array(
"MB" => "WE",
"SI" => "SA",
"SO" => "SA",
);
foreach($first_array as $key1 => $data)
{
if(isset($second_array[$data[2]]))
{
$first_array[$key1][2]=$second_array[$data[2]];
}
}
print_r($first_array);
What about foreach?
foreach($first_array as &$v)
{
//$v[2] = isset($second_array[$v[2]]) ? $second_array[$v[2]] : $v[2];
if(isset($second_array[$v[2]))
$v[2] = $second_array[$v[2]];
}
print_r($first_array);
I have following array as response from db. I am trying to convert this database response into multidimensional array as per my requirement.
Array
(
[0] => Array
(
[0] => Array
(
[_id] => C10359
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10428
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
[1] => Array
(
[0] => Array
(
[_id] => C10350
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10430
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
)
Now I need to convert above array in following way.
Array
(
[0] => Array
(
[C10359] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10428] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
[1] => Array
(
[C10350] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10430] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
following is way i am trying
array_map(function($arr) {
return $arr[0] ;
},$panel_result);
But it is not working.
Kindly suggest how can I convert in required formate.
This should do the trick :
$arr = array(
array(
array(
'_id' => 'C10359',
'AE' => array
(
89785,
89786,
89857,
89859,
),
),
array(
'_id' => 'C10428',
'AE' => array
(
50191,
50203,
50230,
50244,
),
),
),
);
$output = array();
foreach ($arr as $levelK => $level) {
if(!isset($output[$levelK])){
$output[$levelK] = array();
}
foreach ($level as $subLevel) {
$id = $subLevel['_id'];
if (!isset($output[$levelK][$id])) {
$output[$levelK][$id] = array();
}
foreach ($subLevel['AE'] as $val) {
$output[$levelK][$id][] = $val;
}
}
}
Hope this helps.
Use array_column() and pass third param as the index key.
$reqArray = array();
foreach ($yourArray as $key => $innerArray) {
$reqArray[] = array_column($innerArray, 'AE', '_id');
}
OR
Use array map()
$reqArray = array_map(function($a){
return array_column($a, 'AE', '_id');
},$arr);
I have an array with this structure:
Array
(
[mysite] => Array
(
[0] => Array
(
[0] => uniqueid
[1] => brand
[2] => horsepower
[3] => topspeed
)
[1] => Array
(
[0] => uniqueid
[1] => brand
[2] => horsepower
[3] => topspeed
)
[2] => Array
(
[0] => uniqueid
[1] => brand
[2] => horsepower
[3] => topspeed
)
)
)
Every car has an "uniqueid" followed by brand, horsepower, etc.
If I want to get the information for a random car, I do it like this:
$rkey = array_rand($sites['mysite'], 1); // get a random key
$car_info = $myarray['mysite'][$rkey];
Do you have any ideas on how to get information by using a certain "uniqueid"
$car_info = "get the information of a car with a certain uniqueid";
Ty!
try,
Pass unique id inside the for loop you will get the desird result
It may help you,
$array = array(
0 => Array
(
0 => 'uniqueid0',
1 => 'brand',
2 => 'horsepower',
3 => 'topspeed',
),
1 => Array
(
0 => 'uniqueid1',
1 => 'brand',
2 => 'horsepower',
3 => 'topspeed',
),
2 => Array
(
0 => 'uniqueid2',
1 => 'brand',
2 => 'horsepower',
3 => 'topspeed',
),
3 => Array
(
0 => 'uniqueid3',
1 => 'brand',
2 => 'horsepower',
3 => 'topspeed',
),
);
foreach($array as $arr){
if($arr[0] == 'uniqueid2'){
$result = $arr;
break;
}
}
print_r($result );
output:
Array ( [0] => uniqueid2 [1] => brand [2] => horsepower [3] => topspeed )
if the uniqueid is unique across cars then you can try restructure the array as follows
if possible
Array (
[mysite] => Array
(
[uniqueid1] => Array
(
[0] => uniqueid1
[1] => brand
[2] => horsepower
[3] => topspeed
)
[uniqueid2] => Array
(
[0] => uniqueid2
[1] => brand
[2] => horsepower
[3] => topspeed
)
[uniqueid3] => Array
(
[0] => uniqueid3
[1] => brand
[2] => horsepower
[3] => topspeed
)
)
)
this way you can directly access the car for uniqueid and can also apply random logic
if not #Jevgeni Bogatyrjov solution works well
foreach ($myarray['mysite'] as $car) {
if($car[0] == $uniqueid) {
$car_info = $car;
break;
}
}
$uniqueid = 12345;
foreach ($myarray['mysite'] as $car) {
if($car[0] == $uniqueid) {
$car_info = $car;
break;
}
}
i have created a function in php to convert an string like:
[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 20110924, [20110926, 20110927], 20110929]
to php array like:
Array
(
[0] => 20110911
[1] => 20110913
[2] => Array
(
[0] => 20110915
[1] => 20110918
)
[3] => 20110920
[4] => 20110922
[5] => 20110924
[6] => Array
(
[0] => 20110926
[1] => 20110927
)
[7] => 20110929
[8] => Array
(
[0] => 20111001
[1] => 20111002
)
[9] => 20111004
[10] => Array
(
[0] => 20111006
[1] => 20111007
)
)
The function is:
function dates2Array($d){
if($d!==''){
$d=substr($d, 1, strlen($d)-2);
$d=explode(', ', $d);
$dates=array();
if(!empty($d)){
$j=1;
foreach($d as $k=>$v){
if(substr($v, 0, 1)==='[') $dates[]=array(substr($v, 1, strlen($v)));
elseif(substr($v, strlen($v)-1, strlen($v))===']'){
$dates[$k-$j][1]=substr($v, 0, strlen($v)-1);
$j++;
}
else $dates[]=$v;
}
}
}
return $d!==''?$dates:'';
}
I am not fully happy with my function.
I think it can be more optimized and compressed for speed..
Can it be?
Use JSON (json_decode() and json_encode()) instead
http://sandbox.phpcode.eu/g/b3814/2
result:
Array
(
[0] => 20110911
[1] => 20110913
[2] => Array
(
[0] => 20110915
[1] => 20110918
)
[3] => 20110920
[4] => 20110922
[5] => 20110924
[6] => Array
(
[0] => 20110926
[1] => 20110927
)
[7] => 20110929
)
You can pass your string in {...} and pass it to json_decode(str, true) to get back an array.
>> json_decode("[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 2
0110924, [20110926, 20110927], 20110929]")
array (
0 => 20110911,
1 => 20110913,
2 =>
array (
0 => 20110915,
1 => 20110918,
),
3 => 20110920,
4 => 20110922,
5 => 20110924,
6 =>
array (
0 => 20110926,
1 => 20110927,
),
7 => 20110929,
)