Asked  8 Months ago    Answers:  5   Viewed   40 times

I'm trying to create an array inside an array, using a for loop - here's my code:

    array(
    'label' => 'Assign to user',
    'desc' => 'Choose a user',
    'id' => $prefix.'client',
    'type' => 'radio'
    'options' => array( 
        foreach ($clients as $user) {
         $user->user_login => array (  
            'label' => $user->user_login,  
            'value' => $user->user_login,
            ), 
        }
        )
    )

Unfortunately this gives me a

"Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')'"

For the line:

'options' => array( 

I'm at a bit of a loss as to what has gone wrong - any help is much appreciated. $clients is defined elsewhere, so that is not the problem.

 Answers

48

That's invalid syntax. You'd have to build the "parent" portions of the array first. THEN add in the sub-array stuff with the foreach loop:

$foo = array(
    'label' => 'Assign to user',
    'desc' => 'Choose a user',
    'id' => $prefix.'client',
    'type' => 'radio',
    'options' => array()
);

foreach ($clients as $user) {
    $foo['options'][] = array (  
        'label' => $user->user_login,  
        'value' => $user->user_login,
    );
}
Wednesday, March 31, 2021
 
mopsyd
answered 8 Months ago
24

Something like this should work:

$count = count($data['product']);
for ($i = 0; $i < $count; $i ++) {
    echo $data['product'][$i] . ' ' . $data['date'][$i] . ' ' .$data['time'][$i] . '<br />';
}

To add data-verification/integrity to it (to stop undefined index errors), also check if each index exists in the sub-arrays:

$count = count($data['product']);
for ($i = 0; $i < $count; $i++) {
    $date = isset($data['date'][$i]) ? ' ' . $data['date'][$i] : '';
    $time = isset($data['time '][$i]) ? ' ' . $data['time '][$i] : '';
    echo $data['product'][$i] . $date . $time . '<br />';
}
Wednesday, March 31, 2021
 
aaronhuisinga
answered 8 Months ago
32

Move the table tags inside the IF statements

<?php foreach($credits as $credit) : ?>
    <?php if($credit['credit_type'] == "short") : ?>
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td><?php echo $credit['category_position']; ?></td>
            <td><?php echo $credit['category_title']; ?></td>
        </tr>
        <tr>
            <td><?php echo $credit['credit_heading']; ?></td>
            <td><a href="">Edit</a></td>
        </tr>
      </table>
    <?php endif; ?>
    <?php if($credit['credit_type'] == "long") : ?>
       <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td><?php echo $credit['category_position']; ?></td>
            <td><?php echo $credit['category_title']; ?></td>
            <td><strong>Title</strong></td>
            <td><strong>Role</strong></td>
            <td><strong>Director</strong></td>
        </tr>
        <tr>
            <td><?php echo $credit['credit_position']; ?></td>
            <td><?php echo $credit['credit_heading']; ?></td>
            <td><?php echo $credit['credit_title']; ?></td>
            <td><?php echo $credit['credit_role']; ?></td>
            <td><?php echo $credit['credit_director']; ?></td>
        </tr>
       </table>
    <?php endif; ?>
<?php endforeach; ?>

Note this is only going to work if you only ever have 2 credit_type values

Wednesday, March 31, 2021
 
weegee
answered 8 Months ago
93

I am not a wp coder and been years I used it for a demo! You can use key like this, atleast how I would do in php.

    $a = array(
      array(
        'id' => 5698,
        'first_name' => 'Peter',
        'last_name' => 'Griffin',
      ),
      array(
        'id' => 4767,
        'first_name' => 'Ben',
        'last_name' => 'Smith',
      ),
      array(
        'id' => 3809,
        'first_name' => 'Joe',
        'last_name' => 'Doe',
      )
    );
//Collect array values excrated from foreach
    $Collected_array_result = array();
    foreach($a as $key => $value ) {
        $Collected_array_result[':'.$key] = $value;
    }
   //Create another array from that values 
    print_r($Collected_array_result);

Output

Array ( [:0] => Array ( [id] => 5698 [first_name] => Peter [last_name] => Griffin ) [:1] => Array ( [id] => 4767 [first_name] => Ben [last_name] => Smith ) [:2] => Array ( [id] => 3809 [first_name] => Joe [last_name] => Doe ) );

And how to put in db

$stmt = $pdo->prepare("INSERT INTO comments ( " . implode(', ',array_keys($a)) . ") VALUES (" . implode(', ',array_keys($Collected_array_result)) . ")");
$result = $stmt->execute($Collected_array_result);

Get names from array and create a new array with names:

$first_name = array_column($a, 'first_name', 'id');
print_r($first_name);

output

Array ( [5698] => Peter [4767] => Ben [3809] => Joe );

UPDATE : On @Dharman comment for sql injection and insert data in db using prepared statement, wasnt asked for insert query in question but in case you use that query, please filter values from array or use like following.

$first_name = array_column($a, 'first_name');
$first = implode(', ', $first_name);
 echo $first;

$last_names = array_column($a, 'last_name');
$last = implode(', ', $last_names);
 echo $last;

$id = array_column($a, 'id');
$iddd = implode(', ', $id);
 echo $iddd;

$sql = "INSERT INTO comments (first_name, last_names) VALUES (?,?)";
$stmt= $pdo->prepare($sql);
$stmt->execute([$first, $last]);

imploded all values in array and added to query 1 by 1.

Friday, May 28, 2021
 
LukeP
answered 5 Months ago
52

You need to iterate the elements from the inside array, like this:

foreach($resultArray as $row => $innerArray){
  foreach($innerArray as $innerRow => $value){
    echo $value . "<br/>";
  }
}
Wednesday, August 4, 2021
 
Cam1989
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :