How to change the array in following scenario? - php

I've an SQL query as follows:
SELECT test_user_user_id FROM OCN.tests_users WHERE test_user_test_id = 99
$this->mDb->Query( $sql );
$students_data = $this->mDb->FetchArray();
I get the array as follows:
Array
(
[0] => Array
(
[test_user_user_id] => b9e6493f9f8599bc5a0a5935275228c2
)
[1] => Array
(
[test_user_user_id] => 395599c5891c1418357e2efa89bc3e27
)
[2] => Array
(
[test_user_user_id] => 3255605bb9fd3ecc0295a1bfb3cba147
)
[3] => Array
(
[test_user_user_id] => ebe6711fc156b2cc1a33f64f3d86150f
)
[4] => Array
(
[test_user_user_id] => 627b9c3f21d93f1e13af076cff20b143
)
[5] => Array
(
[test_user_user_id] => 030e96561c01afde1c46384f57cf8749
)
[6] => Array
(
[test_user_user_id] => 9def02e6337b888d6dbe5617a172c18d
)
)
Actually I want the array in following format:
Array
(
[0] => b9e6493f9f8599bc5a0a5935275228c2
[1] => 395599c5891c1418357e2efa89bc3e27
[2] => 3255605bb9fd3ecc0295a1bfb3cba147
[3] => ebe6711fc156b2cc1a33f64f3d86150f
[4] => 627b9c3f21d93f1e13af076cff20b143
[5] => 030e96561c01afde1c46384f57cf8749
[6] => 9def02e6337b888d6dbe5617a172c18d
)
How to get the array in above format?

$resArray = Array();
foreach($myArr as $_arr)
{
$resArray[] = $_arr['test_user_user_id'];
}

You could use array_map:
function user_id($e)
{
return $e['test_user_user_id'];
}
$students_data = array_map('user_id', $students_data);

$new_array = array();
$counter = 0; // optional... because by default starting index is 0... however if you want to change the index then use a counter
foreach($students_data as $row) {
$new_array[$counter] = $row['test_user_user_id'];
$counter++;
}
print_r($new_array);

Related

Using odbc_fetch_array but got "arrayarrayarray "instead

I try to get value by using odbc_fetch_array and turn them into variable but when i try to echo it's just said "arrayarrayarray"
Array (
[0] => Array ( [PKDSEQ] => 154604 )
[1] => Array ( [PKDSEQ] => 154604 )
[2] => Array ( [PKDSEQ] => 154529 )
[3] => Array ( [PKDSEQ] => 161689 )
[4] => Array ( [PKDSEQ] => 158940 )
[5] => Array ( [PKDSEQ] => 155383 )
[6] => Array ( [PKDSEQ] => 156247 )
[7] => Array ( [PKDSEQ] => 158123 )
)
and is there a way to get array separate into number?
Code
$PKDSEQ2 = array();
$table4 = "SELECT [PKDSEQ] FROM [PWSWMS].[dbo].[tbTR_PACKD] WHERE [PKDSEQ] = '$PKDSEQRS5'";
$RS4 = odbc_exec($connection2, $table4);
while ($PKDSEQ2 = odbc_fetch_array($RS4)) {
$PKDSEQ[] = $PKDSEQ2;
}
}
print_r(array_values($PKDSEQ));
if(isset($_POST['QTYINPUT1'])) {
$QTYINPUT1 = $_POST['QTYINPUT1'];
$update = "UPDATE [PWSWMS].[dbo].[tbTR_PACKD] SET QTYPCK='$QTYINPUT1' WHERE [PKDSEQ]='$PKDSEQ[1]'";
$result = odbc_exec($connection2, $update);
echo "<br>$QTYINPUT1";
echo "<br>$PKDSEQ[1]";
}
You've got an associative array within each entry in $PKDSEQ. So you need to refer to the property of that array which you want to output. Since the array happens to only have one property, it's pretty simple to choose:
echo "<br>".$PKDSEQ[1]["PKDSEQ"];

PHP push multidimensional array

I got some data from mySQL (Name & Score) and want so push it into an array so I can sort it.
$stack = array();
while ($row = mysqli_fetch_array( $db_erg, MYSQLI_BOTH))
{
$stack_data = array($row['Name'] => $row['Score']);
array_push($stack, $stack_data);
}
//asort($stack);
print_r($stack);
I didn't work to sort it. And when I print my $stack Array it looks like this:
Array ( [0] => Array ( [Nina] => 94 ) [1] => Array ( [Tina] => 50 ) [2] => Array ( [Tim] => 50 ) [3] => Array ( [Anton] => 50 ) [4] => Array ( [Jim] => 50 ) [5] => Array ( [Tom] => 50 ) [6] => Array ( [Ed] => 50 ) [7] => Array ( [Bob] => 50 ) )
Do the sorting with your query:
SELECT `Name`,`Score` FROM `[tablename]` WHERE `Score`>=50 ORDER BY `Score` DESC,`Name`

Change key of multidimensional 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);

Bulid for loop based on given array

I am generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_nameā€¦
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}

Array values update

I have an array like this..
Array
(
[0] => Array
(
[0] => 1``
[1] => 2``
[2] => 3``
)
[1] => Array
(
[0] => 4``
[1] => 5``
[2] => 6``
)
[2] => Array
(
[0] =>
[1] => 7``
[2] =>
)
)
I want the result like this below,
$remaining_value = Array
(
[0] => 1`` 4``,
[1] => 2`` 5`` 7``,
[2] => 3`` 6``,
)
How to do this in an single loop.. Plz help me..
If the lower-level arrays will always have the same number of elements then you can do something like this:
$subArrayCount = count( $inputArray );
$outputArray = array();
$firstSubArray = reset( $inputArray );
foreach( $firstSubArray as $key => $value )
{
$outputArray[$key] = $value;
for( $innerLoop = 1; $innerLoop < $subArrayCount; $innerLoop++ )
{
$outputArray[$key].= $inputArray[$innerLoop][$key];
}
}
var_dump( $outputArray );
This should work:
<?php
$remaining_value=array();
foreach($array as $loopv1){
foreach($loopv1 as $key2 => $loopv2){
if(empty($remaining_value[$key2]))$remaining_value[$key2]=$loopv2; else $remaining_value[$key2].=" ".$loopv2;
}
}
?>

Categories