PHP - Modify an array during foreach loop - php

public function getUserList($user_id){
$list_query = "SELECT ACTUAL_USER_ID FROM TABLE WHERE USER_ID = ".$user_id." ";
$result = parent::queryresult($list_query);
return $result;
}
foreach ($result as $uresult){ //$result contains user_id
$userlist[] = $classObject->getUserList($uresult['USER_ID']);
}
The loop outouts below
Array
(
[0] => Array
(
[0] => Array
(
[ACTUAL_USER_ID] => 133
)
)
[1] => Array
(
[0] => Array
(
[ACTUAL_USER_ID] => 122
)
)
)
How can I remove nested arrays and make it like below
Array
(
[0] => Array
(
[ACTUAL_USER_ID] => 133
)
[1] => Array
(
[ACTUAL_USER_ID] => 122
)
)

Use brackets [] to access array's elements
foreach ($result as $uresult){ //$result contains user_id
$userlist[] = $classObject->getUserList($uresult['USER_ID'])[0];
}
Edit: on different/old version of PHP you need to put array into variable first:
foreach ($result as $uresult){ //$result contains user_id
$row = $classObject->getUserList($uresult['USER_ID']);
$userlist[] = $row[0];
}

Change foreach statement as below
foreach ($result as $uresult){ //$result contains user_id
$res = $classObject->getUserList($uresult['USER_ID']);
if(isset($res[0]))
$userlist[] = $res[0];
}

Related

PHP array and foreach

This is my current code:
$exceptions = array();
foreach ($rows as $row) {
$opens = $row['opens'];
$closes = $row['closes'];
$joined = array($opens, $closes);
$exception = join('-', $joined);
$exceptions[] = array (
$row['date'] => array($exception),
);
}
Which gives:
Array ( [0] => Array ( [06/09] => Array ( [0] => 01:00-22:00 ) ) [1] => Array ( [06/10] => Array ( [0] => 01:00-22:00 ) ) )
But I'm aiming for this because the plugin requires this form:
Array ( [06/09] => Array ( [0] => 01:00-22:00 ) [06/10] => Array ( [0] => 01:00-22:00 ) )
Is there a way to rearrange the array to achieve this?
// Assumptions
// 1. You have `$first_exception` within scope
// 2. You have `$rows` within scope
$exceptions = array();
foreach ($rows as $row) {
// Assumption: `$row` has key `date`
$exceptions[$row['date']] = array (
$first_exception
);
}
Try this:
$exceptions = array();
foreach ($rows as $row) {
$exceptions[$row['date']] = array ($first_exception);
}
You could try this code. And try to read about arrays http://php.net/manual/en/book.array.php
$exceptions = array();
foreach ($rows as $row) {
$exceptions[$row['date']][] = array($first_exception);
}

Append new key to an existing array in an object

I have an existing JSON array:
stdClass Object
(
[set] => Array
(
[0] => stdClass Object
(
[name] => agenda
)
[1] => stdClass Object
(
[name] => first aid
)
)
)
I need to add a new key to it, so the final JSON result is something like this:
set: [{
name: 'agenda',
value: 'Agenda'
}, {
code: 'first aid',
value: 'First Aid'
}],
This is what I've done so far:
$result = array();
foreach ($data->set as $k => $row) {
$result['name'][$k] = $row->name;
$result['value'][$k] = ucwords($row->name);
}
But I have ended up with:
Array
(
[name] => Array
(
[0] => agenda
[1] => aid kit
)
[value] => Array
(
[0] => Agenda
[0] => First Aid
)
)
How can I merge the above so the name and value keys are in pair rather then being separate?
Update $row directly to modify your existing $data:
foreach ($data->set as $row) {
$row->value = ucwords($row->name);
}
To do it the way you are attempting:
$result = $data;
foreach ($data->set as $k => $row) {
$result->set[$k]->value = ucwords($row->name);
}
Notice, $row is an object.
Your try this
$result = array();
$count = 0;
foreach ($data->set as $k => $row) {
$result[$count]['name'] = $row->name;
$result[$count]['value'] = ucwords($row->name);
$count++;
}
result this
Array
(
[0] => stdClass Object
(
[name] => agenda
[value] => Agenda
)
[1] => stdClass Object
(
[name] => first aid
[value] => First Aid
)
)
If you want to modify your existing object, you can just set the value property directly.
foreach ($data->set as $row) {
$row->value = ucwords($row->name);
}
If you want to create a result without modifying your original object or leaving references to its internal objects, you can clone each of the internal objects and add the new properties to the cloned objects.
$result = array();
foreach ($data->set as $k => $row) {
$obj = clone $row;
$obj->value = ucwords($row->name);
$result[$k] = $obj;
}

Fetch all rows based on the query into an array and return single value

Fetch all rows based on the query into an array and return single value
Query database for data
//----------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName"); query
$array = mysql_fetch_row($result);
fetch result In Array
$arr = array();
while ($row = mysql_fetch_assoc($result)) {
$arr2 = array();
foreach ($row as $val) $arr2[] = $val;
$arr[] = $arr2;
}
Result Will Be
Array
(
[0] => Array
(
[0] => status_site
[1] => 0
)
[1] => Array
(
[0] => title_site
[1] => Script
)
[2] => Array
(
[0] => keys_site
[1] =>
)
)
I need to make function that return element 0 to 1
ex: function getsetting (title_site){
return value script}
Replace the $arr[] = $arr2; with $arr[$arr2[0]] = $arr2[1];. That might solve your problem.

creating multidimensional array from single array

i have a html form in which i use array like this (name="courts[]"). when it send data to php file i use foreeach loop to create multidimensional array for inserting records in mysql. In php file i write foreeach loop to iterate like this
$data = array();
$i = 0;
foreach ($court_name as $result)
{
$data[] = array(
'court_name' => $result[0]
);
$i++;
}
it display result this
Array
(
[0] => Array
(
[court_name] => P
)
[1] => Array
(
[court_name] => S
)
)
instead of this
Array
(
[0] => Array
(
[court_name] => Punjab
)
[1] => Array
(
[court_name] => Sindh
)
)
(referring the outputs) in your loop, $result contains court name. So if you use $result[0], you get first character of string.
Try this:
foreach ($court_name as $result)
{
$data[] = array(
'court_name' => $result
);
$i++;
}
foreach loop gives you one element of array ($result) now you access to the first character of value via $result[0], change it to $result
foreach ($court_name as $result) {
$data[] = array( 'court_name' => $result );
}

Creating An Associative Multi Dimensional Array from loop in PHP

How can I create an array like the following in PHP from a database result set using a loop:
Array
(
[T] => Array
(
[0] => Array
(
[id] => 1
[name] => Timer
)
[1] => Array
(
[id] => 2
[name] => Tub
)
)
[P] => Array
(
[0] => Array
(
[id] => 3
[name] => Paper
)
[1] => Array
(
[id] => 4
[name] => Puppy
)
)
)
You will notice that the array keys are a letter, which is taken from the 'name' value in the result set. The loop will be something like this:
while($result = $db->fetch($query) {
$key = $result['name']{0};
// your answer :-)
}
I think something like this should do it:
$sql = 'SELECT id, name FROM table';
$result = mysql_query( $sql);
$answer = array();
while( $row = mysql_fetch_assoc( $result))
{
$answer[ strtoupper($row['name'][0]) ][] = $row;
}
mysql_free_result( $result);
var_dump( $answer);
OR, to be more specific (if your query is returning more columns than just id and name):
while( $row = mysql_fetch_assoc( $result))
{
$answer[ strtoupper($row['name'][0]) ][] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
$indexArray = array(); // Array from Example
while($result = $db->fetch($query) {
$key = $result['name']{0};
if(!isset($indexArray[$key])) {
$indexArray[$key] = array();
}
array_push($indexArray[$key], $result);
}
$results = array();
while($result = $db->fetch($query)) {
$key = strtoupper($result['name'][0]);
if(!isset($results[$key]))
$results[$key] = array();
$results[$key][] = $result;
}

Categories