6

I am rewrting a newsletter-thingy.

I have a table for all newsletter fields, that is like this:

field_uid | field_name | field_content | field_letter_id 

Where f.x. the column field_name can have the value letter_headline and the column field_content can have the value My headline.

When I do this:

$fields = $this->db->dbh->query("SELECT field_name, field_content FROM newsletter_fields WHERE field_letter_uid = '". $letter_id ."'"); 

foreach($fields->fetchAll(PDO::FETCH_ASSOC) as $key) {
   print_r($key);
}

It will correctly give me this:

Array ( [field_name] => letter_image0 [field_content] => image.jpg ) 
Array ( [field_name] => letter_headline [field_content] => My headline ) 
Array ( [field_name] => letter_headline_size [field_content] => 12 ) 
Array ( [field_name] => letter_sub_headline [field_content] => My subheadline ) 
Array ( [field_name] => letter_sub_headline_size [field_content] => 10 ) 
Array ( [field_name] => letter_content [field_content] => Letter content ) 
Array ( [field_name] => letter_link [field_content] => www.example.com )
Array ( [field_name] => letter_link_txt [field_content] => Example )

What I want is to build an array like this

$field["letter_image"] = "image.jpg";
$field["letter_headline"] = "My headline"

And then I can output the content with:

echo $field["letter_image"];

But I can't seem to figure out how to set the field array.

Morten Hagh
  • 2,055
  • 8
  • 34
  • 66
  • 2 questions: - Is `letter_image` always followed by a number? - Do you want your `$field` array to be consistent (ie, are your `letter_image` and `letter_headline` linked)? – Sylvain Oct 11 '13 at 09:24

6 Answers6

19
$result = array_combine(array_column($data, 'field_name'), $data);
digjack
  • 285
  • 1
  • 4
  • 9
  • 1
    Add some explanation to your answer for better understanding. – Pankaj Makwana Mar 07 '18 at 10:27
  • 3
    Although your code snippet might solve the issue, you should describe what’s the purpose of your code (how it solves the problem). Furthermore, you might want to check https://stackoverflow.com/help/how-to-answer – Ahmad F Mar 07 '18 at 10:37
  • 2
    @AhmadF Read the code, it's not very hard to understand. `array_combine` takes two arrays and combines them, uses the first array as the keys and the second array as the values. `array_column` extracts a column out of an array. This is a great solution! – IluTov Aug 08 '18 at 09:01
  • 1
    Best answer! array_column - extract values from special col in rows to array. array_combine - take keys from first array and values from second. –  Jan 10 '19 at 14:47
6

This is an old question, but I think it is worth mentioning that [array_column][1] function can accept the name of the column to use as array keys as a third parameter, so the easiest solution for this is :

// field_content column will be used as values of the array
// field_name column will be used as keys
$result = array_column($data, 'field_content', 'field_name');

Applying this to the example of @Toskan's answer:

$x = [['id' => 5, 'name' => 'john'], ['id' => 15, 'name' => 'jim']];
$result = array_column($x, 'name', 'id');

print_r($result);
// outputs Array ( [5] => john [15] => jim )
Yacine al
  • 163
  • 2
  • 8
5

Easiest solution is just iterate through your array, like:

$result = [];
foreach($data as $row)
{
   $result[$row['field_name']] = $row['field_content'];
}

You'll probably want to do this in your fetch cycle (so you'll avoid then iterating through rows twice)

Alma Do
  • 37,009
  • 9
  • 76
  • 105
4

well, almas solution is not using the native implementation that is optimized.

I am not saying it is wrong, just not optimized.

This should be optimized:

  $x = [['id' => 5, 'name' => 'john'], ['id' => 15, 'name' => 'jim']];

  $keys = array_column($x, 'id');
  $values = array_column($x, 'name');
  $result = array_combine($keys, $values);

  var_dump($result);

//outputs array(2) { [5]=> string(4) "john" [15]=> string(3) "jim" }

This does not work with PHP 4 and only works php > 5.5

Richie Hughes
  • 130
  • 10
Toskan
  • 13,911
  • 14
  • 95
  • 185
1

Use the third parameter of array_column

$rows = [
    [
        'field_name' => 'letter_image',
        'field_content' => 'image.jpg'
    ],
    [
        'field_name' => 'letter_headline',
        'field_content' => 'My headline'
    ],
];

$fields = array_column($rows, 'field_content', 'field_name');

---

php > var_dump($fields);
array(2) {
  ["letter_image"]=>  string(9) "image.jpg"
  ["letter_headline"]=>  string(11) "My headline"
}

https://www.php.net/manual/es/function.array-column.php

paco vela
  • 11
  • 1
-2

Garh... Nevermind..

Fairly simple

$field[$key["field_name"]] = $key["field_content"];
Morten Hagh
  • 2,055
  • 8
  • 34
  • 66